package com.example.comiccenter.service.implt;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.comiccenter.dao.ChapterMapper;
import com.example.comiccenter.dao.ComicMapper;
import com.example.comiccenter.dao.PageMapper;
import com.example.comiccenter.domain.Chapter;
import com.example.comiccenter.domain.Comic;
import com.example.comiccenter.domain.ComicPage;
import com.example.comiccenter.service.ChapterService;
import com.example.comiccenter.utils.ResultData;
import com.example.comiccenter.utils.SearchBean;
import org.apache.commons.io.FileUtils;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

import static com.example.comiccenter.utils.Consts.rootPath;
import static com.example.comiccenter.utils.Consts.targetRootPath;

@Service
public class ChapterServiceImpl implements ChapterService {

    @Resource
    private ChapterMapper chapterMapper;
    @Resource
    private ComicMapper comicMapper;
    @Resource
    private PageMapper pageMapper;

    @Override
    public ResultData getAllChapter(SearchBean<Chapter> searchBean) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("comicName",searchBean.getData().getComicName())
                .eq(searchBean.getData().getChapterNumber()!=null,"chapterNumber",searchBean.getData().getPageNumber());
        List<Chapter> chapters = chapterMapper.selectPage(new  Page<>(searchBean.getPageNum(), searchBean.getPageSize()), wrapper).getRecords();
        return new ResultData(200,"查询成功",chapters);
    }

    @Override
    public ResultData addChapter(Chapter chapter) {
        String comicName = chapter.getComicName();
        QueryWrapper<Comic> comicWrapper = new QueryWrapper<>();
        comicWrapper.eq("name",comicName);
        Long comicNumber = comicMapper.selectCount(comicWrapper);
        if (comicNumber == 0){
            return ResultData.error(401,"该漫画不存在");
        }

        //检查该章节是否已经存在
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.clear();
        wrapper
                .eq("comicName",chapter.getComicName())
                .eq("chapterNumber",chapter.getChapterNumber());
        Long count =  chapterMapper.selectCount(wrapper);
        if (count > 0){
            return ResultData.error(401,"该章节已经存在");
        }else {
            String folderName = "chapter_" + chapter.getChapterNumber().toString();
            String folderPath = rootPath + "\\comic\\" + chapter.getComicName() + "\\" + folderName + "\\";
            String targetFolderPath = targetRootPath + "\\comic\\" + chapter.getComicName() + "\\" + folderName + "\\";

            String path = "\\images\\comic\\" + chapter.getComicName() + "\\" + folderName + "\\";
//            System.out.println("增加章节,path为: "+path);
            new File(folderPath).mkdir();
            new File(targetFolderPath).mkdir();
            chapter.setChapterStorage(path);
            int insert = chapterMapper.insert(chapter);
//            System.out.println("创建章节目录："+insert);
            return ResultData.success("新增章节成功");
        }
    }

    @Override
    public ResultData deleteChapter(Chapter chapter) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper
                .eq("comicName",chapter.getComicName())
                .eq("chapterNumber",chapter.getChapterNumber());
        Integer pageNumber = chapterMapper.selectOne(wrapper).getPageNumber();
        int i = chapterMapper.delete(wrapper);
        String chapterPath = rootPath + "comic\\" + chapter.getComicName() + "\\chapter_" + chapter.getChapterNumber();
        String targetChapterPath = targetRootPath + "comic\\" + chapter.getComicName() + "\\chapter_" + chapter.getChapterNumber();

        File chapterFile = new File(chapterPath);
        File targetChapterFile = new File(targetChapterPath);
        if (chapterFile.exists()) {
            try {
                FileUtils.deleteDirectory(chapterFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (targetChapterFile.exists()) {
            try {
                FileUtils.deleteDirectory(targetChapterFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //删除该章节下的page,
        QueryWrapper<ComicPage> wrapper1 = new QueryWrapper<>();
        wrapper1
                .eq("comicName",chapter.getComicName())
                .eq("chapterNumber",chapter.getChapterNumber());
        pageMapper.delete(wrapper1);

        //更新作品的页数
        QueryWrapper<Comic> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("name",chapter.getComicName());
        Comic comic = comicMapper.selectOne(wrapper2);
        comic.setPages(comic.getPages()-pageNumber);
        comicMapper.updateById(comic);

        return ResultData.success("删除成功");
    }

    @Override
    public ResultData updateChapter(Chapter chapter) {
        int i = chapterMapper.updateById(chapter);
        return ResultData.success("修改成功");
    }
}
